package init

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"sync"
	"user_srv/basic/config"
)

func InMysql() {
	var one sync.Once
	var err error
	data := config.AppConf.Mysql
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", data.User, data.Password, data.Host, data.Port, data.DataBase)
	one.Do(func() {
		config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	})

	if err != nil {
		panic("mysql init failed")
	} else {
		log.Println("mysql init success")
	}
}
